package centers

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/centers"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    centersReq "github.com/flipped-aurora/gin-vue-admin/server/model/centers/request"
)

type CentersService struct {
}

// CreateCenters 创建Centers记录
// Author [piexlmax](https://github.com/piexlmax)
func (centerService *CentersService) CreateCenters(center *centers.Centers) (err error) {
	err = global.GVA_DB.Create(center).Error
	return err
}

// DeleteCenters 删除Centers记录
// Author [piexlmax](https://github.com/piexlmax)
func (centerService *CentersService)DeleteCenters(center centers.Centers) (err error) {
	err = global.GVA_DB.Delete(&center).Error
	return err
}

// DeleteCentersByIds 批量删除Centers记录
// Author [piexlmax](https://github.com/piexlmax)
func (centerService *CentersService)DeleteCentersByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]centers.Centers{},"id in ?",ids.Ids).Error
	return err
}

// UpdateCenters 更新Centers记录
// Author [piexlmax](https://github.com/piexlmax)
func (centerService *CentersService)UpdateCenters(center centers.Centers) (err error) {
	err = global.GVA_DB.Save(&center).Error
	return err
}

// GetCenters 根据id获取Centers记录
// Author [piexlmax](https://github.com/piexlmax)
func (centerService *CentersService)GetCenters(id uint) (center centers.Centers, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&center).Error
	return
}

// GetCentersInfoList 分页获取Centers记录
// Author [piexlmax](https://github.com/piexlmax)
func (centerService *CentersService)GetCentersInfoList(info centersReq.CentersSearch) (list []centers.Centers, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&centers.Centers{})
    var centers []centers.Centers
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&centers).Error
	return  centers, total, err
}
